#!/bin/sh

set -x

prefix=$1
destdir=$2
erl=$3
werl=$4
etcdir=$5
vardir=$6
bindir=$7

#bindir=`dirname $erl` ; bindir=`dirname $bindir`/lib/erlang/bin

. ../vsn.mk
y=yaws-${YAWS_VSN}
p=${prefix}
e=${etcdir}
v=${vardir}

install -d ${destdir}/${prefix}/bin
install -d ${destdir}/${prefix}/etc

cat yaws.template | \
    ./Subst %yawsdir% ${prefix}/lib/yaws | \
    ./Subst %vardir% ${vardir} | \
    ./Subst %erl% "${erl}" | \
    ./Subst %run_erl% "${bindir}/run_erl" | \
    ./Subst %to_erl% "${bindir}/to_erl" | \
    ./Subst %werl% "${werl}" > ${destdir}/${prefix}/bin/yaws

chmod +x ${destdir}/${prefix}/bin/yaws

install -d ${destdir}/${prefix}/lib/yaws/examples/ebin
install -d ${destdir}/${prefix}/lib/yaws/examples/include

if [ -f /etc/gentoo-release ]; then
    install -d ${destdir}/${etcdir}/init.d/
    install -d ${destdir}/${etcdir}/conf.d/
    install -d ${destdir}/${vardir}/run/yaws
    chmod a+rwx ${destdir}/${vardir}/run/yaws
    cp gentoo/init.d.yaws  ${destdir}/${etcdir}/init.d/yaws
    chmod +x ${destdir}/${etcdir}/init.d/yaws
    sed -e "s;%prefix%;$p;g" gentoo/conf.d.yaws > ${destdir}/${etcdir}/conf.d/yaws
elif [ -f /etc/redhat-release ]; then
    install -d ${destdir}/${etcdir}/init.d
    sed -e "s;%prefix%;$p;g" redhat/yaws.init.d  > ${destdir}/${etcdir}/init.d/yaws
    chmod +x ${destdir}/${etcdir}/init.d/yaws
    install -d ${destdir}/${vardir}/run/yaws
    chmod a+rwx ${destdir}/${vardir}/run/yaws
elif [ -f /etc/suseservers ]; then
    install -d ${destdir}/${etcdir}/init.d
    sed -e "s;%prefix%;$p;g" suse/yaws.init.d  > ${destdir}/${etcdir}/init.d/yaws
    chmod +x ${destdir}/${etcdir}/init.d/yaws
    install -d ${destdir}/${vardir}/run/yaws
    chmod a+rwx ${destdir}/${vardir}/run/yaws
elif [ -f /etc/debian_version ]; then
    install -d ${destdir}/${etcdir}/init.d
    sed -e "s;%prefix%;$p;g" debian/yaws.init.d  > ${destdir}/${etcdir}/init.d/yaws
    chmod +x ${destdir}/${etcdir}/init.d/yaws
    install -d ${destdir}/${vardir}/run/yaws
    chmod a+rwx ${destdir}/${vardir}/run/yaws

elif [ "`uname -s`" = "Darwin" -a `id -u` = 0 ]; then
     startupdir="/Library/StartupItems/Yaws"
     if [ ! -e ${startupdir} ]; then
       mkdir ${startupdir};
     elif [ ! -d ${startupdir} ]; then
	 echo "${startupdir} exists but is not a directory, bailing out ..."
	 exit 1
     fi
     sed -e "s;%prefix%;$p;g" darwin/Yaws.StartupItem > ${startupdir}/Yaws
     chmod +x ${startupdir}/Yaws
     cp darwin/Yaws.plist ${startupdir}/StartupParameters.plist
      # MacOS is particular about the ownership of startup items.
     chown -R root:wheel ${startupdir}
elif [ "`uname -s`" = "FreeBSD" ]; then
    sed -e "s;%prefix%;$p;g" -e "s;%etcdir%;$e;g" freebsd/yaws.sh > ${destdir}/${etcdir}/rc.d/yaws.sh
elif [ "`uname -s`" = "NetBSD" ]; then
    sed -e "s;%prefix%;$p;g" -e "s;%etcdir%;$e;g" netbsd/yaws.sh > /etc/rc.d/yaws
else    
    install -d ${destdir}/${etcdir} 
    echo "Don't know how to make /etc/init scrips for this system"
    echo "possibly add ${prefix}/bin/yaws --daemon --heart to your /etc/rc.local manually"
fi






